<template>
  <!-- 放置一个图标 -->
  <div>
    <svg-icon
      :icon-class="isFull ? 'exit-fullscreen' : 'fullscreen'"
      class="fullscreen"
      @click="toggleScreen"
    />
  </div>
</template>

<script>
// 导入ScreenFull包
import sf from 'screenfull'
export default {
  name: 'ScreenFull',
  data () {
    return {
      // 是否是全屏响应数据
      isFull: false
    }
  },
  created () {
    this.onScreen()
  },
  beforeDestroy () {
    // 关闭监控\ => 解绑事件
    sf.off('change')
  },
  methods: {
    // 监控浏览器是否是全屏状态
    onScreen () {
      sf.on('change', () => {
        // sf.isFullscreen true全屏状态,相反就是关闭全屏
        if (sf.isFullscreen) {
          this.isFull = true
        } else {
          this.isFull = false
        }
      })
    },

    // 开启或关闭全屏
    // sf.isEnabled true 浏览器支持;否 不支持
    toggleScreen () {
      if (!sf.isEnabled) {
        this.$message.error('浏览器不支持全屏功能! 换个浏览器试试吧~')
      }
      sf.toggle()
    }
  }
}
</script>

<style lang="scss" scoped>
.fullscreen {
  width: 20px;
  height: 20px;
  color: #fff;
  cursor: pointer;
}
</style>
